package com.kaiwukj.android.ufamily.mvp.model;

import android.app.Application;

import com.google.gson.Gson;
import com.kaiwukj.android.mcas.integration.IRepositoryManager;
import com.kaiwukj.android.mcas.mvp.BaseModel;
import com.kaiwukj.android.ufamily.mvp.contract.PayFeeContract;
import com.kaiwukj.android.ufamily.mvp.http.api.service.PayFeeService;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.BaseListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.BaseObjResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.event.PayInfo;
import com.kaiwukj.android.ufamily.mvp.http.entity.requestbak.PayFeeCancelRequest;
import com.kaiwukj.android.ufamily.mvp.http.entity.requestbak.PayFeeListRequest;
import com.kaiwukj.android.ufamily.mvp.http.entity.requestbak.PayFeeRequest;
import com.kaiwukj.android.ufamily.mvp.http.entity.resultbak.AlipayResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.resultbak.PayFeeListResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.resultbak.PayFeeResult;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

/**
 * @author Charles
 * @date 2019/8/14.
 * description：Kaiwu
 */
public class PayFeeModel extends BaseModel implements PayFeeContract.Model {

    @Inject
    Gson mGson;
    @Inject
    Application mApplication;

    @Inject
    public PayFeeModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Observable<BaseListResp<PayFeeListResult>> requestPayFeeList(PayFeeListRequest request) {
        return mRepositoryManager.obtainRetrofitService(PayFeeService.class)
                .requestPayFeeList(getRequestBody(mGson.toJson(request)));
    }

    @Override
    public Observable<BaseListResp<PayFeeResult>> requestPayFeeLists(String reqJson) {
        return Observable.just(mRepositoryManager.obtainRetrofitService(PayFeeService.class).requestPayFeeLists(getRequestBody(reqJson)))
                .flatMap((Function<Observable<BaseListResp<PayFeeResult>>,
                        ObservableSource<BaseListResp<PayFeeResult>>>) observable -> observable);
    }

    @Override
    public Observable<BaseObjResp<PayInfo>> payByWechat(PayFeeRequest request) {
        return mRepositoryManager.obtainRetrofitService(PayFeeService.class)
                .payByWechat(getRequestBody(mGson.toJson(request)));
    }

    @Override
    public Observable<BaseObjResp<AlipayResult>> payByAli(PayFeeRequest request) {
        return mRepositoryManager.obtainRetrofitService(PayFeeService.class)
                .payByAli(getRequestBody(mGson.toJson(request)));
    }

    @Override
    public Observable<BaseObjResp<Object>> cancelOrder(PayFeeCancelRequest request) {
        return mRepositoryManager.obtainRetrofitService(PayFeeService.class)
                .cancelOrder(getRequestBody(mGson.toJson(request)));
    }

}
